package org.itli.jnsocket.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.itli.jnsocket.Pojo.OnBound;
import org.itli.jnsocket.Result.Result;
import org.itli.jnsocket.Service.OnBoundService;
import org.itli.jnsocket.Util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@Tag(name = "入库操作")
@RequestMapping("/OnBound")
@RestController
public class OnBoundController {

    OnBoundService service;

    JWTUtil jwt = new JWTUtil();

    @Autowired
    public void setService(OnBoundService service) {
        this.service = service;
    }

    //入库操作
    @PostMapping("/On")
    @Operation(summary = "销售入库")
    public Result<String> OutBound(@RequestHeader("Authorization") String token,@RequestBody OnBound onBound) {
        log.info("接收到数据:"+onBound.toString());
        Map<String, Object> cliam;
        try{
            cliam = jwt.parseToken(token);
        } catch (Exception e) {
            return Result.success("令牌过期");
        }
        String username = (String) cliam.get("username");
        String s = service.OutBounds(username, onBound);
        return Result.success(s);
    }
}
